package pers.lonestar.framework.utils.support.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;

import pers.lonestar.framework.utils.support.enums.CellDataTypeEnum;

public class CellStyleUtil {
    public static CellStyle getHeadStyle(Workbook workbook) {
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        CellStyle headStyle = workbook.createCellStyle();
        headStyle.setBorderLeft(BorderStyle.MEDIUM);
        headStyle.setBorderRight(BorderStyle.MEDIUM);
        headStyle.setBorderTop(BorderStyle.MEDIUM);
        headStyle.setBorderBottom(BorderStyle.MEDIUM);
        headStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headStyle.setAlignment(HorizontalAlignment.LEFT);
        headStyle.setFont(font);
        headStyle.setDataFormat((short) 0x31);
        return headStyle;
    }

    public static Map<CellDataTypeEnum, CellStyle> getContentStyleMap(Workbook workbook) {
        Map<CellDataTypeEnum, CellStyle> contentStyleMap = new HashMap<>();
        DataFormat dataFormat = workbook.createDataFormat();
        Font font = createDefaultFont(workbook);

        CellStyle dateStyle = createDefaultCellStyle(workbook, font);
        dateStyle.setDataFormat(dataFormat.getFormat("yyyy-MM-dd"));
        CellStyle stringStyle = createDefaultCellStyle(workbook, font);
        stringStyle.setDataFormat(dataFormat.getFormat("@"));
        CellStyle defaultStyle = createDefaultCellStyle(workbook, font);

        contentStyleMap.put(CellDataTypeEnum.DATE, dateStyle);
        contentStyleMap.put(CellDataTypeEnum.STRING, stringStyle);
        contentStyleMap.put(CellDataTypeEnum.DIRECT_STRING, stringStyle);
        contentStyleMap.put(CellDataTypeEnum.NUMBER, defaultStyle);
        contentStyleMap.put(CellDataTypeEnum.BOOLEAN, defaultStyle);
        contentStyleMap.put(CellDataTypeEnum.EMPTY, defaultStyle);
        contentStyleMap.put(CellDataTypeEnum.ERROR, defaultStyle);

        contentStyleMap.put(null, defaultStyle);
        return contentStyleMap;
    }

    private static CellStyle createDefaultCellStyle(Workbook workbook, Font font) {
        CellStyle defalutStyle = workbook.createCellStyle();
        defalutStyle.setBorderLeft(BorderStyle.THIN);
        defalutStyle.setBorderRight(BorderStyle.THIN);
        defalutStyle.setBorderTop(BorderStyle.THIN);
        defalutStyle.setBorderBottom(BorderStyle.THIN);
        defalutStyle.setAlignment(HorizontalAlignment.LEFT);
        defalutStyle.setFont(font);
        return defalutStyle;
    }

    private static Font createDefaultFont(Workbook workbook) {
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        return font;
    }
}
